package com.car.advertising.activity;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.car.advertising.R;
import com.car.advertising.activity.adapter.RegsiterAdapter;
import com.car.advertising.connection.Contact;
import com.car.advertising.connection.HttpUrl;
import com.car.advertising.entity.bean.RegisterBean;
import com.car.advertising.listener.SaveEditListener;
import com.support.widget.interfacelistener.OnRecyclerViewItemClickListener;
import com.support.widget.layout.BaseRecylerViewToolBarActivity;
import com.support.widget.network.OkHttpClientManager;
import com.support.widget.util.LogUtil;
import com.support.widget.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class RegsiterActivity extends BaseRecylerViewToolBarActivity implements SaveEditListener {

    private List<RegisterBean> registerList = new ArrayList<>();
    private String userName;//用户名
    private String password;//密码
    private String mobile;//手机
    private String email;//邮箱
    private String realName;//真实姓名
    private String cityName;//城市名称
    private String provinceName;//省份名称
    private String address;//详细地址

    @Override
    protected void initToolBar() {
        setToolBarBackgroundColor(R.color.color_white);
        setToolBarTitle("注册");
        setToolBarTitleColor(R.color.black_bg);

        setPullUpEnabled(false);
        setPullDownEnabled(false);
    }

    @Override
    protected void init() {

        registerList = getRegisterList();
        RegsiterAdapter regsiterAdapter = new RegsiterAdapter(this, registerList);
        initRecyclerView(regsiterAdapter);
        regsiterAdapter.setOnItemClickListener(registerItemClick);
    }

    private List<RegisterBean> getRegisterList() {
        List<RegisterBean> arrayList = new ArrayList<>();
        arrayList.add(new RegisterBean(1, "用户名", "请输入用户名"));
        arrayList.add(new RegisterBean(2, "密码", "请输入密码"));
        arrayList.add(new RegisterBean(3, "手机", "请输入手机号码"));
        arrayList.add(new RegisterBean(4, "邮箱", "请输入e-mail"));
        arrayList.add(new RegisterBean(5, "真实姓名", "用于收件"));
        arrayList.add(new RegisterBean(6, "城市名称", "当前城市"));
        arrayList.add(new RegisterBean(7, "省份名称", "当前省份"));
        arrayList.add(new RegisterBean(8, "详细地址", "收件地址"));
        arrayList.add(new RegisterBean(9, "立即注册", ""));
        return arrayList;
    }

    @Override
    public void getEditPositionText(int position, String string) {
        switch (position) {
            case 0:
                userName = string;
                break;
            case 1:
                password = string;
                break;
            case 2:
                mobile = string;
                break;
            case 3:
                email = string;
                break;
            case 4:
                realName = string;
                break;
            case 5:
                cityName = string;
                break;
            case 6:
                provinceName = string;
                break;
            case 7:
                address = string;
                break;
            default:
                break;
        }
    }

    private OnRecyclerViewItemClickListener registerItemClick = new OnRecyclerViewItemClickListener() {
        @Override
        public void onItemClick(ViewGroup parent, View view, int position) {
            if (TextUtils.isEmpty(userName)) {
                ToastUtil.show(RegsiterActivity.this, "用户名不能为空", Toast.LENGTH_SHORT);
            } else if (TextUtils.isEmpty(password)) {
                ToastUtil.show(RegsiterActivity.this, "密码不能为空", Toast.LENGTH_SHORT);
            } else if (password.length() < 6 || password.length() > 20) {
                ToastUtil.show(RegsiterActivity.this, "密码至少在6-20位字符以内", Toast.LENGTH_SHORT);
            } else if (TextUtils.isEmpty(mobile)) {
                ToastUtil.show(RegsiterActivity.this, "手机号不能为空", Toast.LENGTH_SHORT);
            } else {
                regsiter(userName, password, mobile, email, realName, cityName, provinceName, address);
            }
        }
    };

    private void regsiter(String user, String password, String mobile, String email, String realName, String cityName, String provinceName, String address) {
        List<OkHttpClientManager.Param> params = new ArrayList<>();
        params.add(new OkHttpClientManager.Param("userName", user));
        params.add(new OkHttpClientManager.Param("password", password));
        params.add(new OkHttpClientManager.Param("header", "h.jpg"));
        params.add(new OkHttpClientManager.Param("age", "30"));
        params.add(new OkHttpClientManager.Param("sex", "0"));
        params.add(new OkHttpClientManager.Param("cityId", "2"));
        params.add(new OkHttpClientManager.Param("cityName", cityName));
        params.add(new OkHttpClientManager.Param("provinceName", provinceName));
        params.add(new OkHttpClientManager.Param("provinceId", "110000"));
        params.add(new OkHttpClientManager.Param("address", address));
        params.add(new OkHttpClientManager.Param("mobile", mobile));
        params.add(new OkHttpClientManager.Param("email", email));
        params.add(new OkHttpClientManager.Param("contant", realName));
        OkHttpClientManager.postAsyn(HttpUrl.getInstance().URL_036, new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(String request) {
                ToastUtil.show(RegsiterActivity.this, "注册失败", Toast.LENGTH_SHORT);
                LogUtil.i(request);
            }

            @Override
            public void onResponse(String resp) {
                ToastUtil.show(RegsiterActivity.this, "注册成功", Toast.LENGTH_SHORT);
                Contact.saveToken(resp);
                finish();
            }

            @Override
            public void onFinish() {

            }
        }, params, null);
    }


    @Override
    protected void onRefreshFinish() {

    }

    @Override
    protected void onLoadMoreFinish() {

    }

    @Override
    public String getCN() {
        return null;
    }

    @Override
    public void LogoutCallback(boolean islogin) {

    }

    @Override
    public void onCritClick(View v) {

    }

    @Override
    protected boolean isClickable() {
        return false;
    }

    @Override
    protected void subtitleClick() {

    }

    @Override
    protected void titleClick() {

    }

    @Override
    protected void rightClick() {

    }

}
